<?php

namespace app\queue;

use support\Redis;
use app\model\Order;
use think\facade\Db;
use Webman\RedisQueue\Consumer;

class SingleGoodsConsume implements Consumer
{
    // 单商品入库操作
    public $queue = 'purchase_sign_goods';

    // 连接名，对应 plugin/webman/redis-queue/redis.php 里的连接`
    public $connection = 'default';

    // 消费
    public function consume($data)
    {
        $goods_info['id']   = $data['goods_id'];
        $goods_info['name'] = '测试商品';

        
        Db::startTrans();
        try{
            //入库操作
            echo '商品id'.$data['goods_id'].'入库成功';
            //操作数据库实际库存减相应个数
            echo '商品id'.$data['goods_id'].'库存减少'.$data['buy_nums'];
            
        
            // 提交事务
            Db::commit();
            
        }catch(\Exception $e)
        {
            //如果失败了redis库存再加回来
            Redis::incSingleGoodsStock($data['goods_id'], $data['buy_nums']);

            // 回滚事务
            Db::rollback();
        }
    }
}
